#extension GL_OES_EGL_image_external : require
precision mediump float;

varying vec2 vCoordinate;
varying vec4 vColor;

uniform samplerExternalOES uTexture;
uniform int uColorFilter;
uniform int uScreenSplit;

void main() {
    vec2 uv = vCoordinate;
    if (uScreenSplit == 1) {
        // 0.0～0.5范围内显示0.25～0.75范围的像素
        if (uv.y >= 0.0 && uv.y <= 0.5) {
            uv.y = uv.y + 0.25;
        } else {
            // 0.5～1.0范围内显示0.25～0.75范围的像素
            uv.y = uv.y - 0.25;
        }
    } else if (uScreenSplit == 2) {
        if (uv.y <= 0.3333) {
            uv.y = uv.y + 0.3333;
        } else if (uv.y <= 0.6667) {

        } else {
            uv.y = uv.y - 0.3333;
        }
    } else if (uScreenSplit == 3) {
        if (uv.x <= 0.5) {
            uv.x = uv.x * 2.0;
        } else {
            uv.x = (uv.x - 0.5) * 2.0;
        }

        if (uv.y <= 0.5) {
            uv.y = uv.y * 2.0;
        } else {
            uv.y = (uv.y - 0.5) * 2.0;
        }
    } else if (uScreenSplit == 4) {
        if (uv.x <= 1.0 / 3.0) {
            uv.x = uv.x * 3.0;
        } else if (uv.x <= 2.0 / 3.0) {
            uv.x = (uv.x - 1.0 / 3.0) * 3.0;
        } else {
            uv.x = (uv.x - 2.0 / 3.0) * 3.0;
        }

        if (uv.y <= 1.0 / 3.0) {
            uv.y = uv.y * 3.0;
        } else if (uv.y <= 2.0 / 3.0) {
            uv.y = (uv.y - 1.0 / 3.0) * 3.0;
        } else {
            uv.y = (uv.y - 2.0 / 3.0) * 3.0;
        }
    }

    vec4 color = texture2D(uTexture, uv);
    if (uColorFilter == 1) {
        // 黑白图片
        float gray = 0.3 * color.r + 0.59 * color.g + 0.11 * color.b;
        color = vec4(gray, gray, gray, 1.0);

        gl_FragColor = color;
    } else if (uColorFilter == 2) {
        gl_FragColor = color + vColor;
    } else {
        gl_FragColor = color;
    }
}